package com.my.java.io.fileio;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

public class FileIOUtil {

	public static void main(String[] args) {
		List<String> strlist = readFile2StringList(new File("src/com/my/java/io/fileio/fileRes/ioTest.txt"));
		System.out.println(strlist);
		try {
			writeFileByStringList("src/com/my/java/io/fileio/fileRes/ioTest2.txt", strlist, true);
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 以字节为单位读取文件，常用于读二进制文件，如图片、声音、影像等文件。
	 */
	private  static void readFileByBytes(String fileName) {
		File file = new File(fileName);
		InputStream in = null;
		try {
			// 一次读一个字节
			in = new FileInputStream(file);
			int tempbyte;
			while ((tempbyte = in.read()) != -1) {
				System.out.println(tempbyte);
			}
			in.close();
		}
		catch (IOException e) {
			e.printStackTrace();
			return;
		}

		try {
			System.out.println("以字节为单位读取文件内容，一次读多个字节：");
			// 一次读多个字节
			byte[] tempbytes = new byte[100];
			int byteread = 0;
			in = new FileInputStream(fileName);
			FileIOUtil.showAvailableBytes(in);
			// 读入多个字节到字节数组中，byteread为一次读入的字节数
			while ((byteread = in.read(tempbytes)) != -1) {
				System.out.write(tempbytes, 0, byteread);
				System.out.println(tempbytes.toString());
			}
			System.out.println("");
		}
		catch (Exception e1) {
			e1.printStackTrace();
		}
		finally {
			if (in != null) {
				try {
					in.close();
				}
				catch (IOException e1) {
				}
			}
		}

		Reader reader = null;
		try {
			System.out.println("以字符为单位读取文件内容，一次读一个字节：");
			// 一次读一个字符
			reader = new InputStreamReader(new FileInputStream(file));
			int tempchar;
			while ((tempchar = reader.read()) != -1) {
				// 对于windows下，\r\n这两个字符在一起时，表示一个换行。
				// 但如果这两个字符分开显示时，会换两次行。
				// 因此，屏蔽掉\r，或者屏蔽\n。否则，将会多出很多空行。
				if (((char) tempchar) != '\r') {
					System.out.print((char) tempchar);
				}
			}
			reader.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 显示输入流中还剩的字节数
	 */
	private static void showAvailableBytes(InputStream in) {
		try {
			System.out.println("当前字节输入流中的字节数为:" + in.available());
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 按行读取数据
	 * 
	 * @return
	 */
	public static List<String> readFile2StringList(File file) {
		List<String> strList = new ArrayList<String>();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				strList.add(tempString);
			}
			reader.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			if (reader != null) {
				try {
					reader.close();
				}
				catch (IOException e1) {
				}
			}
		}
		return strList;
	}

	/**
	 * 按行写入文件
	 * @return
	 * @throws IOException 
	 */
	public static void writeFileByStringList(String fileName, List<String> strList, boolean isAppend) throws IOException {
			FileWriter writer = new FileWriter(fileName, isAppend);
			for (String str : strList) {
				writer.write(str);
				writer.close();
			}
	}
	
/**
 *  按行写入文件（默认追加形式）
 * @param fileName
 * @param strList
 * @throws IOException
 */
	public static void writeFileByStringList(String fileName, List<String> strList) throws IOException {
		 writeFileByStringList(fileName, strList, true);
	}
	
	public static List<String> getFileNamesInFolder(String folderPath){
		List<String> filenameList = new ArrayList<String>();
		  File folder=new File(folderPath);
		  File[] fileList = folder.listFiles();
		  for(File file:fileList){
			  filenameList.add(file.getName());
		  }
		return filenameList;
	}
	
}
